using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Data;
using PSLibrary = Microsoft.Office.Project.Server.Library;

namespace MicrosoftFrance.MCS.EPM2007
{
    class PSIProject
    {
        public static void PublishProjectList(List<Guid> projectList)
        {
            for (int p = 0; p < projectList.Count; p++)
            {
                Guid jobUid = Guid.NewGuid();
                Program.project.QueuePublish(jobUid, projectList[p], false, string.Empty);
            }
        }

        public static Guid CreateProject(ProjectWS.ProjectDataSet project, string projectname)
        {
            ProjectWS.ProjectDataSet.ProjectRow prjrow = project.Project.NewProjectRow();
            prjrow.PROJ_UID = Guid.NewGuid();
            prjrow.PROJ_TYPE = 0;
            prjrow.PROJ_NAME = projectname;
            prjrow.PROJ_INFO_START_DATE = new DateTime(2006, 01, 01);
            prjrow.PROJ_OPT_DEF_TASK_TYPE = 1; //fixed duration
            prjrow.PROJ_OPT_MINUTES_PER_DAY = 480; //8 heures par jour
            prjrow.PROJ_OPT_MINUTES_PER_WEEK = 2400; //5 jours par semaine
            prjrow.PROJ_OPT_SHOW_EST_DUR = false;
            prjrow.PROJ_OPT_NEW_TASKS_ARE_EFFORT_DRIVEN = false;

            project.Project.AddProjectRow(prjrow);
            return prjrow.PROJ_UID;
        }

        public static void UpdateProjectStartDate(Guid prjID, DateTime startdate)
        {
            ProjectWS.ProjectDataSet project = Program.project.ReadProject(prjID, ProjectWS.DataStoreEnum.WorkingStore);
            if (project != null)
            {
                project.Project[0].PROJ_INFO_START_DATE = startdate;
            }
        }

        public static Guid GetProjectByName(string projname)
        {
            Guid projID = Guid.Empty;
            ProjectWS.ProjectDataSet prjDataSet = new ProjectWS.ProjectDataSet();
            prjDataSet = Program.project.ReadProjectList();
            prjDataSet.Project.DefaultView.RowFilter = "PROJ_NAME ='" + projname + "'";
            if (prjDataSet.Project.DefaultView.Count > 0)
            {
                projID = new Guid(prjDataSet.Project.DefaultView[0]["PROJ_UID"].ToString());
            }
            return projID;
        }

        public static Guid GetProjectByName(string projname, ProjectWS.ProjectDataSet prjDataSet)
        {
            Guid projID = Guid.Empty;
            prjDataSet.Project.DefaultView.RowFilter = "PROJ_NAME ='" + projname + "'";
            if (prjDataSet.Project.DefaultView.Count > 0)
            {
                projID = new Guid(prjDataSet.Project.DefaultView[0]["PROJ_UID"].ToString());
            }
            return projID;
        }

        public static Guid GetProjectSummaryTask(Guid prjID)
        {
            ProjectWS.ProjectDataSet prjDataSet = new ProjectWS.ProjectDataSet();
            prjDataSet = Program.project.ReadProjectEntities(prjID, 2, ProjectWS.DataStoreEnum.WorkingStore);
            prjDataSet.Task.DefaultView.RowFilter = "TASK_UID = TASK_PARENT_UID";
            return new Guid(prjDataSet.Task.DefaultView[0]["TASK_UID"].ToString());
        }

        public static Guid FindProjectCustomFieldIDByName(Guid prjID, string cfname, DataTable projectCF)
        {
            Guid cfGuid = Guid.Empty;
            if (projectCF != null)
            {
                projectCF.DefaultView.RowFilter = "PROJ_UID = '" + prjID.ToString() +
                    "' AND MD_PROP_UID = '" + PSICustomFields.GetMD_PROP_UID(cfname) + "'";
                if (projectCF.DefaultView.Count > 0)
                {
                    cfGuid = new Guid(projectCF.DefaultView[0]["CUSTOM_FIELD_UID"].ToString());
                }
            }
            return cfGuid;
        }

    }
}
